﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WPF.MDI;

namespace Net5ApiANdWpfClient.WpfClient.Common
{
    public class WindowsManager
    {
        public static W GetWindowInCurrentWindows<W>() where W : class
        {
            if (Application.Current.Windows == null) return null;
            W window = null;
            foreach (var w in Application.Current.Windows)
            {
                if (w.GetType() == typeof(W))
                {
                    window = w as W;
                }

            }
            if (window == null) return null;
            return window;
        }

        public static void MdiWindowShowChildShow(Type type, MdiContainer mdiContainer,string title)
        {
            MdiChild nowForm = null;
            foreach (MdiChild form in mdiContainer.Children)
            {
                if (form.Content.GetType().Equals(type))
                {
                    nowForm = form;
                }
            }
            if (nowForm == null)
            {
                var nowControl = type.Assembly.CreateInstance(type.FullName) as UIElement;

                nowForm = new MdiChild
                {
                    Title = title,
                    Content = nowControl
                };
                //  nowForm.Width = nowControl.Width+50;
                //   nowForm.Height = nowControl.Height+50;
                nowForm.MaximizeBox = true;
                nowForm.MinimizeBox = true;
                
                nowForm.CloseBox = true;
                mdiContainer.Children.Add(nowForm);

            }
        }
    }
}
